#include "Aliyun.h"


Aliyun::Aliyun(QObject *parent)
    :QObject (parent)
{

}

QByteArray Aliyun::connectAliyun(QStringList arguments)
{

    QProcess process;
    process.start("python", arguments);
    process.waitForFinished();
    QByteArray result = process.readAllStandardOutput();
    //qDebug()<<result;
    return result;
}



QString Aliyun::pocessJson(QByteArray result)
{

    //将ByterArray用本地的utf编码
    QString output = QString::fromLocal8Bit(result);
    QJsonDocument res = QJsonDocument::fromJson(output.toUtf8());
    QJsonObject jsonObject = res.object();
    QJsonArray choices = jsonObject.value("choices").toArray();
    QJsonObject choice = choices.at(0).toObject();
    QJsonObject message = choice.value("message").toObject();
    QString content = message.value("content").toString().replace("\n","");

    parseJsonData(content);
    return content;
}

QJsonObject Aliyun::parseJsonData(QString const &jsonString)
{
    QString cleanedString = jsonString;
    cleanedString.replace("\\\"", "\"");

    // 确保移除了```json和```标记
    if (cleanedString.startsWith("```json"))
        cleanedString = cleanedString.mid(7);
    if (cleanedString.endsWith("```"))
        cleanedString = cleanedString.left(cleanedString.length() - 3);
    QJsonDocument doc = QJsonDocument::fromJson(cleanedString.toUtf8());
    if (!doc.isNull() && doc.isObject()) {
        QJsonObject obj = doc.object();
            qDebug()<<"json为:"<< obj;
        // 现在可以访问各个字段
//        QString seller_name = obj["seller_name"].toString();
//        QString buyer_name = obj["buyer_name"].toString();
//        QString invoice_number = obj["invoice_number"].toString();
//        QString invoice_date = obj["invoice_date"].toString();
//        float total_amount = obj["total_amount"].toDouble();
//        float tax_amount = obj["tax_amount"].toDouble();

//        qDebug() << "卖方名称:" << seller_name;
//        qDebug() << "买方名称:" << buyer_name;
//        qDebug() << "发票号码:" << invoice_number;
//        qDebug() << "发票日期:" << invoice_date;
//        qDebug() << "总金额:" << total_amount;
//        qDebug() << "税额:" << tax_amount;
        return obj;
        } else {
            qDebug() << "JSON解析失败!";
            return QJsonObject();
        }
}

QString Aliyun::content(const QJsonObject &obj)
{
    QString str = ("seller_name:"+obj["seller_name"].toString()+"、");
    str.append("buyer_name:"+obj["buyer_name"].toString()+"、");
    str.append("invoice_number:"+obj["invoice_number"].toString()+"、");
    str.append("invoice_date:"+obj["invoice_date"].toString()+"、");
    float total_amount = obj["total_amount"].toDouble();
    str.append("total_amount:"+QString::number(total_amount)+"、");
    float tax_amount = obj["tax_amount"].toDouble();
    str.append("tax_amount:"+QString::number(tax_amount)+".");
    return str;
}

QString Aliyun::sql(const QString &content)
{
    QRegularExpression re("INSERT INTO.*?VALUES.*?\\);");
    QRegularExpressionMatch match = re.match(content);
    QString sqlStatement;
    if (match.hasMatch()) {
        sqlStatement = match.captured(0);
        qDebug() << "提取的SQL语句:" << sqlStatement;
    }
    return sqlStatement;
}



